/*
 * Cell.cpp
 *
 *  Created on: 04/01/2012
 *      Author: luiz
 */

#include <sstream>

#include "Cell.h"

Cell::Cell(int id):cell_id_(id){
    cell_function_ = NORMAL_CELL;
    cell_clock_zone_ = CLOCK_ZONE_0;
    cell_polarization_ = POLARIZATION_0;
    cell_name_ = "";
}

Cell::Cell(const Cell & cell)
{
    cell_clock_zone_ = cell.cell_clock_zone_;
    cell_function_ = cell.cell_function_;
    cell_id_ = cell.cell_id_;
    cell_name_ = cell.cell_name_;
    cell_position_ = cell.cell_position_;
    cell_polarization_ = cell.cell_polarization_;
}

Cell & Cell::operator =(const Cell & cell)
{
    if(this == &cell){
        return *this;
    }

    cell_clock_zone_ = cell.cell_clock_zone_;
    cell_function_ = cell.cell_function_;
    cell_id_ = cell.cell_id_;
    cell_name_ = cell.cell_name_;
    cell_position_ = cell.cell_position_;
    cell_polarization_ = cell.cell_polarization_;
    return *this;
}


Cell::~Cell() {

}

void Cell::set_cell_function(CELL_FUNCTION cell_type)
{
    cell_function_ = cell_type;
}

void Cell::set_cell_id(int id)
{
    cell_id_ = id;
}

Cell::CELL_FUNCTION Cell::cell_function()
{
    return cell_function_;
}

int Cell::cell_id()
{
    return cell_id_;
}

void Cell::set_cell_clock_zone(CLOCK_ZONE zone)
{
    cell_clock_zone_ = zone;
}

Cell::CLOCK_ZONE Cell::cell_clock_zone()
{
    return cell_clock_zone_;
}


Position Cell::cell_position() const
{
    return cell_position_;
}

int Cell::cell_position_horizontal()
{
    return cell_position_.x();
}

int Cell::cell_position_vertical()
{
    return cell_position_.y();
}

void Cell::set_cell_position(const Position & position)
{
    cell_position_ = position;
}

void Cell::set_cell_position(int horizontal_position, int vertical_position)
{
    cell_position_ = Position(horizontal_position,vertical_position);
}


Cell::CELL_POLARIZATION Cell::cell_polarization() const
{
    return cell_polarization_;
}

void Cell::set_cell_polarization(CELL_POLARIZATION cellPolarization)
{
    cell_polarization_ = cellPolarization;
}

void Cell::set_cell_name(const std::string & name)
{
    cell_name_ = name;
}

std::string Cell::cell_name()
{
    return cell_name_;
}


std::string Cell::print_cell()
{
    std::stringstream oss;
    oss << "[ " << "(" << cell_position_.x() << "," << cell_position_.y() << ")  F:" << cell_function_  << "  Z:" << cell_clock_zone_  <<  " P:" << cell_polarization_  <<" ]" ;
    return oss.str();
}

bool Cell::operator ==(const Cell& cell) const
                {
    if ( (this->cell_position_ == cell.cell_position_) &&
            this->cell_function_ == cell.cell_function_ &&
            this->cell_clock_zone_ == cell.cell_clock_zone_ &&
            this->cell_polarization_ == cell.cell_polarization_
    )
    {
        return true;
    }

    return false;
}
/*
 * Priority Order
 *  - Position
 *  - Function
 *  - Clock Zone
 *  - Polarization
 *
 */
bool Cell::operator <(const Cell& cell) const
{
    if(this->cell_position_ <= cell.cell_position_ ){
        if(this->cell_position_ < cell.cell_position_ ){
            return true;
        } else if( this->cell_function_ <= cell.cell_function_ ) {
            if( this->cell_function_ < cell.cell_function_){
                return true;
            } else   if(this->cell_clock_zone_ <= cell.cell_clock_zone_){
                if(this->cell_clock_zone_ < cell.cell_clock_zone_){
                    return true;
                } else if(this->cell_polarization_ <= cell.cell_polarization_){
                    if(this->cell_polarization_ < cell.cell_polarization_){
                        return true;
                    }
                }
            }
        }
    }
    return false;
}

int Cell::compare(const Cell& cell) const
{
    if(this->cell_position_ <= cell.cell_position_ ){
           if(this->cell_position_ < cell.cell_position_ ){
               return -1;
           } else if( this->cell_function_ <= cell.cell_function_ ) {
               if( this->cell_function_ < cell.cell_function_){
                   return -1;
               } else   if(this->cell_clock_zone_ <= cell.cell_clock_zone_){
                   if(this->cell_clock_zone_ < cell.cell_clock_zone_){
                       return -1;
                   } else if(this->cell_polarization_ <= cell.cell_polarization_){
                       if(this->cell_polarization_ < cell.cell_polarization_){
                           return -1;
                       } else {
                           return 0;
                       }
                   }
               }
           }
       }
       return 1;
}

 bool Cell::compare_less_then(Cell *  a, Cell *  b)
{
    if(a->compare(*b) < 0){
        return true;
    }
    return false;
}
